package com.huawei.struts.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.huawei.struts.util.PageBean;
import com.st.model.LjwProgramT;
import com.st.model.LjwVillageT;

/**
 * A data access object (DAO) providing persistence and search support for
 * LjwVillageT entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.st.model.LjwVillageT
 * @author MyEclipse Persistence Tools
 */

public class LjwVillageTDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(LjwVillageTDAO.class);
	// property constants
	public static final String VILLAGE_NAME = "villageName";
	public static final String TELEPHONE = "telephone";
	public static final String REMARK = "remark";
	public static final String VILLAGE_TEMPLATE_ID = "villageTemplateId";

	protected void initDao() {
		// do nothing
	}

	public void save(LjwVillageT transientInstance) {
		log.debug("saving LjwVillageT instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(LjwVillageT persistentInstance) {
		log.debug("deleting LjwVillageT instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public LjwVillageT findById(java.lang.Integer id) {
		log.debug("getting LjwVillageT instance with id: " + id);
		try {
			LjwVillageT instance = (LjwVillageT) getHibernateTemplate().get(
					"com.st.model.LjwVillageT", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(LjwVillageT instance) {
		log.debug("finding LjwVillageT instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding LjwVillageT instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from LjwVillageT as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByVillageName(Object villageName) {
		return findByProperty(VILLAGE_NAME, villageName);
	}

	public List findByTelephone(Object telephone) {
		return findByProperty(TELEPHONE, telephone);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByVillageTemplateId(Object villageTemplateId) {
		return findByProperty(VILLAGE_TEMPLATE_ID, villageTemplateId);
	}

	public List findAll() {
		log.debug("finding all LjwVillageT instances");
		try {
			String queryString = "from LjwVillageT";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public LjwVillageT merge(LjwVillageT detachedInstance) {
		log.debug("merging LjwVillageT instance");
		try {
			LjwVillageT result = (LjwVillageT) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(LjwVillageT instance) {
		log.debug("attaching dirty LjwVillageT instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(LjwVillageT instance) {
		log.debug("attaching clean LjwVillageT instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static LjwVillageTDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (LjwVillageTDAO) ctx.getBean("LjwVillageTDAO");
	}
	
	
	public List queryForPage(final LjwVillageT program,final PageBean pageBean) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				StringBuffer hql=new StringBuffer();
				hql.append("from LjwVillageT u where 1=1");
				Query query=queryMap(hql,program,session);
				query.setFirstResult(pageBean.getFirstResult());
				query.setMaxResults(pageBean.getPageSize());
				List list = query.list();
				return list;
			}
		});
		return list;
	}
	
	public int getAllRowCount(final LjwVillageT program) {
		Integer count = (Integer)getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				StringBuffer hql=new StringBuffer();
				hql.append("select count(*) from LjwVillageT u where 1=1");
				Query query=queryMap(hql,program,session); 
				return  Integer.parseInt(query.iterate().next().toString());
			}
		});
		return Integer.parseInt(count.toString());
	}
	
	public  Query queryMap(final StringBuffer hql,final LjwVillageT program,Session session)
	{
		Map map=new HashMap();

		if(program.getVillageName()!=null){
			map.put("name", "%"+program.getVillageName()+"%");
		    hql.append(" and u.villageName like :name");
		}
		if(program.getRemark()!=null){
			map.put("description", "%"+program.getRemark()+"%");
		    hql.append(" and u.remark like :description");
		}
		Query query = session.createQuery(hql.toString());
		
		Iterator it = map.keySet().iterator();

         while (it.hasNext()){

         Object key = it.next(); 

         query.setParameter(key.toString(), map.get(key)); 

         }
         return query;
	}
}